package com.marsyang.display
{
	import com.greensock.*;
	import com.greensock.easing.*;
	import com.marsyang.model.ModelLocator;
	
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.Event;
	import flash.filters.BlurFilter;
	import flash.geom.Point;
	
	import org.casalib.math.geom.Ellipse;
	import org.papervision3d.core.proto.MaterialObject3D;
	import org.papervision3d.events.InteractiveScene3DEvent;
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.objects.primitives.Plane;
	
	public class PlaneObject extends BaseScene
	{
		private var obj3D:DisplayObject3D;
		private var numPlanes:int = 6;
		private var numLayers:int = 30;
		private var radius:Number = 50;
		private var planes:Array = [];
		
		private var colorsFull:Array = [0x9C8D6C, 0x8E6832, 0x015062, 0x003556, 0x640E00, 0x9E7973, 0x883600, 0xF76300, 0xCF2100, 0xF0F0F0];
		private var colorsGray:Array = [0x677888, 0x22272B, 0x1F2428, 0x0F1012];
		private var colorsFullLen:int;
		private var colorsGrayLen:int;
		
		public var model:ModelLocator = ModelLocator.getInstance();
		
		public function PlaneObject()
		{
			colorsFullLen = colorsFull.length;
			colorsGrayLen = colorsGray.length;
		}
		public function create():void {
			for(var j:int = 0; j < numLayers; j++){
				obj3D = new DisplayObject3D();
				for(var i:int = 0; i < numPlanes; i++)
				{
					var colorMaterial:ColorMaterial = new ColorMaterial(Math.floor(Math.random() * colorsFullLen));
					var plane:Plane = new Plane(colorMaterial, 100-j*2.2, 100-j*2.2);
					var circle:Ellipse = new Ellipse(-radius, -radius, radius * 2, radius * 2);
					var point:Point = circle.getPointOfDegree(360 / numPlanes * i); 
					plane.name = "p"+i;
					plane.x = point.x;
					plane.z = point.y;
					plane.scaleX=.1;
					plane.scaleY=.1;
					plane.addEventListener(InteractiveScene3DEvent.OBJECT_CLICK, planeClickHandler);
					obj3D.addChild(plane);
					scene.addChild(obj3D);
				}
				obj3D.rotationY = Math.random() * 360;
				obj3D.rotationX = Math.random() * 360;
				planes.push(obj3D);
			}
		}
		private function planeClickHandler(e:InteractiveScene3DEvent):void
		{
			TweenLite.to(e.target, 3, {x:e.target.x+100,y:e.target.y+100, z:e.target.z+100,rotationX:e.target.rotationX-90, rotationY:e.target.rotationY-90});
		}		
		public function planesDefEffect():void
		{
			radius = 100;
			for(var j:int = 0; j < numLayers; j++){
				var n:Number = j<(numLayers-10)?(j*1.02+5):(j*1.5+30);
				radius += n;
				for(var i:int = 0; i < numPlanes; i++)
				{
					var p:Plane = planes[j].getChildByName("p"+i) as Plane;
					var circle:Ellipse = new Ellipse(-radius, -radius+50, radius * 2, radius * 2);
					var point:Point = circle.getPointOfDegree(360 / numPlanes * i); 
					TweenLite.to(p, 1, {x:point.x, z:point.y, scaleX:1, scaleY:1, ease:Bounce.easeOut});
				}
				planes[j].rotationY = Math.random() * 360;
				planes[j].rotationX = Math.random() * 360;
			}
		}
		public function planesIn():void
		{
			radius = 20;
			for(var j:int = 0; j < numLayers; j++)
			{
				radius += 20;
				for(var i:int = 0; i < numPlanes; i++)
				{
					var p:Plane = planes[numLayers-1-j].getChildByName("p"+i) as Plane;
					var circle:Ellipse = new Ellipse(-radius, -radius, radius * 2, radius * 2);
					var point:Point = circle.getPointOfDegree(360 / numPlanes * i);
					TweenLite.to(p, 1, {x:point.x, z:point.y, scaleX:0.7, scaleY:0.7});
				}				
			}
			TweenLite.to(this.camera, 1, {z:-1500});
			TweenLite.delayedCall(1, planesInEffect);
		}
		public function planesInEffect():void
		{
			TweenLite.killTweensOf(this.camera);
			model.renderType = "DEF";
		}
		public function planesOut():void
		{
			radius = 20;
			for(var j:int = 0; j < numLayers; j++)
			{
				radius += 30;
				for(var i:int = 0; i < numPlanes; i++)
				{
					var p:Plane = planes[numLayers-1-j].getChildByName("p"+i) as Plane;
					var circle:Ellipse = new Ellipse(-radius+20, -radius, radius * 2, radius * 2);
					var point:Point = circle.getPointOfDegree(360 / numPlanes * i); 
					TweenLite.to(p, 1, {x:point.x, z:point.y});
				}
			}
			TweenLite.to(this.camera, 1, {z:-3000});
			TweenLite.delayedCall(1, planesOutEffect);
		}
		public function planesOutEffect():void
		{
			TweenLite.killTweensOf(this.camera);
			radius = 50;
			clearPlanesColor();
			for(var j:int = 0; j < numLayers; j++){
				var n:Number = j<(numLayers-10)?(j*1.02+5):(j*1.5+30);
				radius += n;
				TweenLite.to(planes[j], 2, {rotationY:Math.floor(Math.random() * 180), rotationX:Math.floor(Math.random() * 180)});				
				for(var i:int = 0; i < numPlanes; i++)
				{
					var p:Plane = planes[j].getChildByName("p"+i) as Plane;
					var circle:Ellipse = new Ellipse(-radius, -radius+50, radius * 2, radius * 2);
					var point:Point = circle.getPointOfDegree(360 / numPlanes * i); 
					TweenLite.to(p, 1, {x:point.x, z:point.y, scaleX:1, scaleY:1});
				}
			}
			TweenLite.to(this.camera, 1, {z:-500});
			
		}
		public function planesRota():void
		{
			var n:int = 0;
			for each(var obj:DisplayObject3D in planes)
			{	
				if(n%2 == 0){
					TweenLite.to(obj, 1, {rotationY:obj.rotationY+model.currentMenu*45});
				}else {
					TweenLite.to(obj, 1, {rotationX:obj.rotationX+model.currentMenu*45});
				}
				n++;
			}
		}
		public function planesOnRender():void
		{
			var n:int = 0;
			for each(var obj:DisplayObject3D in planes)
			{
				if(n%2 == 0){
					obj.rotationX += 5;
				}else {
					obj.rotationY += 5;
				}
				n++;
			}
			TweenLite.to(this.camera, 3, {rotationY:(stage.width/2 - mouseX) * -0.02, rotationX:(stage.height/2 - mouseY) * -0.02});
		}	
		public function setPlanesColor():void
		{
			for(var j:int = 0; j < numLayers; j++)
			{
				for(var i:int = 0; i < numPlanes; i++)
				{
					var p:Plane = planes[j].getChildByName("p"+i) as Plane;
					p.material.interactive = false;
					p.material.fillColor = colorsFull[Math.floor(Math.random() * colorsFullLen)];
				}				
			}
			viewport.filters = [];
		}
		public function clearPlanesColor():void
		{			
			for(var j:int = 0; j < numLayers; j++)
			{
				for(var i:int = 0; i < numPlanes; i++)
				{
					var p:Plane = planes[j].getChildByName("p"+i) as Plane;
					p.material.interactive = true;
					p.material.fillColor = colorsGray[Math.floor(Math.random() * colorsGrayLen)];
				}
			}
			viewport.filters = [new BlurFilter(5,5,1)];
		}
	}
}